package com.minmin.testCases;

import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;

import java.util.Random;

@Slf4j

public class TestProduct extends TestCom {

    public TestProduct(WebDriver driver) {
        super(driver);
    }

    public void testProduct() throws InterruptedException {
        log.info("进入商品相关功能栏");
        eXpath("//ul/li/div/span[text()=\"商品\"]").click();

        log.info("进入商品导入模块");
        eXpath("//*//ul/li/span[text()=\"商品导入\"]").click();
        log.info("测试商品导入");
        testImportProduct();
        log.info("--------------------------------------------");
        Thread.sleep(1000);

        log.info("进入商品管理模块");
        eXpath("//ul/li/span[text()=\"商品管理\"]").click();
        log.info("测试商品");
        testAboutProduct();
        log.info("--------------------------------------------");
        Thread.sleep(1000);

//        System.exit(1);
        log.info("进入分类导入模块");
        eXpath("//ul/li/span[text()=\"分类导入\"]").click();
        log.info("测试分类导入");
        testImportClassify();
        log.info("--------------------------------------------");
        Thread.sleep(1000);

        log.info("进入分类管理模块");
        eXpath("//ul/li/span[text()=\"分类管理\"]").click();
        log.info("测试分类");
        testAboutClassify();
        log.info("--------------------------------------------");
        Thread.sleep(1000);

        log.info("进入规格管理模块");
        eXpath("//ul/li/span[text()=\"规格管理\"]").click();
        testSpecification();
        log.info("--------------------------------------------");
        Thread.sleep(1000);

        log.info("进入品牌管理模块");
        eXpath("//ul/li/span[text()=\"品牌管理\"]").click();
        Thread.sleep(1000);
        testBrand();
        log.info("--------------------------------------------");
        Thread.sleep(1000);


        eXpath("//div/span[text()=\"商品\"]").click();
        Thread.sleep(1000);
    }

    private void testBrand() {
        log.info("新增品牌");
        eXpath("//button/span[text()=\"新增\"]").click();
        eXpath("//input[@placeholder=\"请输入品牌名称\"]").sendKeys("自动化新增品牌");
        eXpath("//input[@placeholder=\"请输入品牌编码\"]").sendKeys("自动化新增品牌");
        eXpath("//input[@placeholder=\"请输入描述\"]").sendKeys("ZDH");
        eXpath("//button/span[text()=\"确定\"]").click();
        log.info("新增品牌成功");
        eXpath("(//span[text()=\"删除\"])[1]").click();
        eXpath("//button/span[text()=\"确定\"]").click();
        log.info("删除品牌成功");
    }

    private void testSpecification() throws InterruptedException {
        log.info("创建规格组");
        eXpath("//button/span[text()=\"新增规格组\"]").click();
        eXpath("//input[@placeholder=\"请输入规格组名称\"]").sendKeys("自动化创建规格组");
        eXpath("//button/span[text()=\"确定\"]").click();
        if (A.alert(driver, "保存成功")) {
            log.info("创建规格组成功");
        } else {
            log.info("创建规格组失败");
        }
        log.info("创建规格");
        Thread.sleep(100);
        eXpath("(//span[text()=\" 添加规格选项\"])[1]").click();
        eXpath("//input[@placeholder=\"请输入规格选项名称\"]").sendKeys("自动化创建规格");
        eXpath("//button/span[text()=\"确定\"]").click();
        if (A.alert(driver, "操作成功")) {
            log.info("创建规格成功");
        } else {
            log.info("创建规格失败");
        }
        Thread.sleep(500);
        log.info("删除规格");
        eXpath("(//span[text()=\" 删除 \"])[2]").click();
        eXpath("//button/span[text()=\"确定\"]").click();
        if (A.alert(driver, "删除成功")) {
            log.info("删除规格成功");
        } else {
            log.info("删除规格失败");
        }

        Thread.sleep(100);
        log.info("删除规格组");
        eXpath("(//span[text()=\" 删除 \"])[1]").click();
        eXpath("//button/span[text()=\"确定\"]").click();
        if (A.alert(driver, "删除成功")) {
            log.info("删除规格组成功");
        } else {
            log.info("删除规格组失败");
        }
    }

    private void testAboutClassify() throws InterruptedException {
        log.info("新增分类");
        eXpath("//button/span[text()=\"新增\"]").click();
        eXpath("//input[@placeholder=\"请输入分类名\"]").sendKeys("Classify");
        eXpath("//input[@placeholder=\"请输入自定义编码\"]").sendKeys("cs");
        eXpath("//button/span[text()=\"确定\"]").click();
        Thread.sleep(500);
        try {
            eXpath("//p[text()=\"保存成功\"]");
            log.info("创建分类成功");
        } catch (Exception e) {
            log.info("创建分类失败");
            log.info("失败原因：{}", e.toString());
        }
        log.info("--------------------------------------------");
        log.info("测试搜索+删除分类");
        log.info("搜索自定义编码为cs的分类");
        eXpath("//input[@placeholder=\"可输入关键字搜索分类名、自定义编码\"]").sendKeys("cs");
//        eXpath("//button/span[text()=\"搜索\"]").click();
        Thread.sleep(500);
        log.info("删除分类");
        eXpath("//button/span[text()=\"删除\"]").click();
        eXpath("//button/span[text()=\"确定\"]").click();
        Thread.sleep(200);
        try {
            eXpath("//p[text()=\"删除成功\"]");
            log.info("删除分类成功");
        } catch (Exception e) {
            log.info("删除分类失败");
            log.info("失败原因：{}", e.toString());
        }
    }

    private void testImportClassify() throws InterruptedException {
        log.info("开始导入分类");
        Thread.sleep(500);
        eXpath("(//div/input)[1]").sendKeys(getBaseUrl() + "/src/main/java/com/minmin/xls/收银系统分类导入模板.xlsx");
        while (true) {
            Thread.sleep(1000);
            eXpath("//button/span[text()=\"刷新进度\"]").click();
            String flog = eXpath("//tr/td[7]/div").getText();
            if (flog.equals("已完成")) {
                Integer fail = Integer.valueOf(eXpath("//tr/td[5]/div").getText());
                if (fail == 0) {
                    log.info("数据全部导入成功");
                } else {
                    log.info("失败了[{}]行数据，请下载失败数据以查看", fail);
                }
                break;
            }
        }
    }

    private void testImportProduct() throws InterruptedException {
        log.info("开始导入商品");
        eXpath("//*[@id=\"app\"]/section/section/main/div/div/div/div/div[1]/div/div[1]/div[1]/div/input").sendKeys(getBaseUrl() + "/src/main/java/com/minmin/xls/收银系统商品报价导入模板.xlsx");
        while (true) {
            Thread.sleep(1000);
            driver.findElement(By.xpath("//*[@id=\"app\"]/section/section/main/div/div/div/div/div[1]/div/div[1]/div[2]/button[2]")).click();
            String flog = eXpath("//*[@id=\"app\"]/section/section/main/div/div/div/div/div[2]/div[1]/div[3]/div/div[1]/div/table/tbody/tr[1]/td[7]/div").getText();
            if (flog.equals("已完成")) {
                int fail = Integer.parseInt(eXpath("//*[@id=\"app\"]/section/section/main/div/div/div/div/div[2]/div[1]/div[3]/div/div[1]/div/table/tbody/tr[1]/td[5]").getText());
                if (fail == 0) {
                    log.info("数据全部导入成功");
                } else {
                    log.info("失败了[{}]行数据，请下载失败数据以查看", fail);
                }
                break;
            }
        }
    }

    private void testAboutProduct() throws InterruptedException {
        log.info("测试新增商品");
        String barcode = String.valueOf(new Random().nextInt(1000000000));
        eXpath("//*[@id=\"app\"]/section/section/main/div/div/div[2]/div/div[1]/div[1]/button[1]").click();
        eXpath("//input[@placeholder=\"请填写商品条码\"]").sendKeys(barcode);
        eXpath("//button/span[text()=\"确定\"]").click();
//        driver.findElement(By.xpath("//input[@placeholder=\"请填写商品名称\"]")).clear();
        eXpath("//input[@placeholder=\"请填写商品名称\"]").sendKeys("testNewProduct");
//        eXpath("(//input[@placeholder=\"请选择分类\"])[last()]").sendKeys("默认");
//        Thread.sleep(1000);
//        eXpath("(//span[text()=\"默认\"])[last()]").click();
        eXpath("//input[@placeholder=\"请填写商品单位\"]").sendKeys("瓶");
        eXpath("(//input[@placeholder=\"请输入\"])[2]").sendKeys("10");
        eXpath("(//input[@placeholder=\"请输入\"])[3]").sendKeys("20");
        eXpath("(//input[@placeholder=\"请输入\"])[4]").sendKeys("15");

        while (true) {
            if (driver.getPageSource().contains("确定")) {
                log.info("信息填写完毕，点击确认按钮创建商品");
                eXpath("//span[text()=\"确定\"]").click();
                break;
            }
            log.info("页面懒加载,滑动页面，找指定元素");
            JavascriptExecutor executor = (JavascriptExecutor) driver;
            executor.executeScript("window.scrollTo(0, document.body.scrollHeight)");
            Thread.sleep(300);
        }
        Thread.sleep(500);
        try {
            eXpath("//p[text()=\"保存成功！\"]");
            log.info("创建商品成功");
        } catch (Exception e) {
            log.info("创建商品失败");
            log.info("失败原因：{}", e.toString());
        }
        Thread.sleep(500);

        log.info("测试更新商品");
        eXpath("(//button/span[text()=\"编辑\"])[1]").click();
        eXpath("//input[@placeholder=\"请填写商品名称\"]").clear();
        eXpath("//input[@placeholder=\"请填写商品名称\"]").sendKeys("testUpdateProduct");
        eXpath("(//input[@placeholder=\"请输入\"])[2]").clear();
        eXpath("(//input[@placeholder=\"请输入\"])[2]").sendKeys("100");
        eXpath("(//input[@placeholder=\"请输入\"])[3]").clear();
        eXpath("(//input[@placeholder=\"请输入\"])[3]").sendKeys("100");
        eXpath("//button/span[text()=\"确定\"]").click();
        eXpath("//button/span[text()=\"是\"]").click();
        Thread.sleep(500);

        try {
            eXpath("//p[text()=\"保存成功！\"]");
            log.info("编辑商品成功");
        } catch (Exception e) {
            log.info("编辑商品失败");
            log.info("失败原因：{}", e.toString());
        }

        log.info("--------------------------------------------");

        log.info("测试删除商品");
        Thread.sleep(500);
        eXpath("//*[@id=\"app\"]/section/aside/div/div[2]/div[1]/div/ul/li[3]/ul/li[3]").click();
        eXpath("(//span[text()=\"删除\"])[1]").click();
        eXpath("//span[text()=\"确定\"]").click();
        Thread.sleep(500);
        try {
            eXpath("//p[text()=\"操作成功\"]");
            log.info("删除商品成功");
        } catch (Exception e) {
            log.info("删除商品失败");
            log.info("失败原因：{}", e.toString());
        }

        log.info("测试搜索商品");
        eXpath("//input[contains(@placeholder,\"条码/商品名称/拼音首字母\")]").sendKeys("666666");

        eXpath("//span[text()=\"搜索\"]").click();
        try {
            if (eXpath("//tr[1]/td[8]/div").getText().equals("666666")) {
                log.info("商品搜索功能正常");
            }
        } catch (Exception e) {
            log.info("搜索商品不存在");
        }
        eXpath("//button/span[text()=\"重置\"]").click();
        log.info("--------------------------------------------");
        Thread.sleep(1000);

        log.info("批量操作商品");
        log.info("批量下架");
        batch("修改商品状态", "下架", 1);
        log.info("商城批量下架");
        batch("商城商品状态", "下架", 2);
        log.info("商城批量上架");
        batch("商城商品状态", "上架", 2);
        log.info("批量删除");
        batch("修改商品状态", "删除", 1);
        Thread.sleep(200);
        log.info("回收站恢复删除商品");
        eXpath("//button/span[text()=\"回收站\"]").click();
        Thread.sleep(500);
        eXpath("(//table/thead/tr/th[1]/div/label/span/span)[last()]").click();
        eXpath("//button/span[text()=\" 恢复 \"]").click();
        if (A.alert(driver, "操作成功")) {
            log.info("回收站恢复商品成功");
        } else {
            log.info("回收站恢复商品失败");
        }
        driver.navigate().refresh();
        log.info("批量上架");
        Thread.sleep(2000);
        batch("修改商品状态", "上架", 1);

        log.info("批量改价");
        eXpath("//table/thead/tr/th[1]/div/label/span/span").click();
        eXpath("//button/span[contains(normalize-space(),\"批量修改\")]").click();
        Thread.sleep(200);
        eXpath("//div[text()=\"修改商品价格\"]").click();
        eXpath("//input[@placeholder=\"修改零售价\"]").sendKeys("100");
        eXpath("//button/span[text()=\"确定\"]").click();
        eXpath("(//button/span[text()=\"确定\"])[last()]").click();
        if (A.alert(driver, "改价成功,请前往收银机同步商品,更新价格")) {
            log.info("批量改价成功");
        } else {
            log.info("批量改价失败");
        }

        log.info("撤销改价");
        eXpath("//button/span[text()=\" 更多操作 \"]").click();
        eXpath("//button/span[text()=\" 撤销改价 \"]").click();
        Thread.sleep(200);
        eXpath("//input[@placeholder=\"请选择改价记录\"]").click();
//        eXpath("//span[text()=\"请选择改价记录\"]").click();
        actions.sendKeys(Keys.DOWN).perform();
        actions.sendKeys(Keys.ENTER).perform();
        eXpath("(//button/span[text()=\"搜索\"])[last()]").click();
        eXpath("//button/span[text()=\" 确定撤销 \"]").click();
        eXpath("//button/span[text()=\"确定\"]").click();
        if (A.alert(driver, "撤销商品改价成功,请前往收银机同步商品,更新价格")) {
            log.info("撤销改价成功");
        } else {
            log.info("撤销改价失败");
        }

    }

    public void batch(String locMessageF, String locMessageC, int num) throws InterruptedException {
        eXpath("//table/thead/tr/th[1]/div/label/span/span").click();
        eXpath("//button/span[contains(normalize-space(),\"批量修改\")]").click();
        Thread.sleep(500);
        eXpath("//div[text()=\"" + locMessageF + "\"]").click();
        Thread.sleep(200);
        eXpath("(//label/span[text()=\"" + locMessageC + "\"])[" + num + "]").click();
        eXpath("(//button/span[text()=\"保存\"])[" + num + "]").click();
        eXpath("(//button/span[text()=\"确定\"])[last()]").click();
        if (A.alert(driver, "操作成功")) {
            log.info("批量上架成功");
        } else {
            log.info("批量上架失败");
        }
        Thread.sleep(200);
    }
}
